#include <iostream>
#include <pthread.h>
#include <vector>
#include <string>
#include <unistd.h>

pthread_mutex_t gmtx = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t gcond = PTHREAD_COND_INITIALIZER;

void *func(void *args)
{
    while (true)
    {
        pthread_mutex_lock(&gmtx);

        // 条件等待
        pthread_cond_wait(&gcond, &gmtx);
        char *name = (char *)args;
        std::cout << name << std::endl;
        pthread_mutex_unlock(&gmtx);
        usleep(100000);
    }

    return nullptr;
}
int main()
{
    std::vector<pthread_t> tids;
    for (int i = 0; i < 10; i ++ )
    {
        char *name = new char[128];
        sprintf(name, "pthread -%d", i + 1);
        pthread_t tid;
        pthread_create(&tid, nullptr, func, name);
    }

    // 唤醒
    while (true)
    {
        pthread_cond_signal(&gcond);
        std::cout << "唤醒一个线程..." << std::endl;
        usleep(100000);
    }
    return 0;
}